package com.activity;

import java.util.ArrayList;





import util.ApplicationConstant;

import com.activity.R;


import com.fragment.LocateDetailsFragment;

import dao.FileMyLocate;
import entities.MyLocate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class GetDirection extends Activity {
	private TextView txtGoto;
	private Spinner spinnerMyLocation;
	private static ArrayList<MyLocate> myLocates;
	private Button btnGetDirection;
	private ArrayList<String> myLocateNames;
	public static Boolean MY_LOCATE_MARK=false;
	//goto locate
	double la,log;
	String name;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_direction);
		txtGoto=(TextView) findViewById(R.id.txt_goto);
		spinnerMyLocation=(Spinner) findViewById(R.id.spinner_mylocation);
		btnGetDirection=(Button) findViewById(R.id.btn_ok_get_direction);
		if(MY_LOCATE_MARK==true)
		{
			la=getIntent().getExtras().getDouble(ApplicationConstant.LAT2);
			log=getIntent().getExtras().getDouble(ApplicationConstant.LOG2);
			name=getIntent().getExtras().getString(ApplicationConstant.NAME2);
			txtGoto.setText(String.format(getString(R.string.go_to),name));
		}
		else
			txtGoto.setText(String.format(getString(R.string.go_to),LocateDetailsFragment.locate.getName()));
		
		FileMyLocate fileMyLocate=new FileMyLocate(getApplicationContext());
		myLocates=fileMyLocate.loadAll();
		myLocateNames=new ArrayList<String>();
		myLocateNames.add(getString(R.string.current_locate));
		
		for(MyLocate locate:myLocates)
			myLocateNames.add(locate.getName());
		ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,myLocateNames);
		spinnerMyLocation.setAdapter(arrayAdapter);
		btnGetDirection.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(GetDirection.this,MapActivity.class);
				//get 2 point to get direction
				int pos=spinnerMyLocation.getSelectedItemPosition();
				double fromLatitute=0;
				double fromLongtitute=0;
				if(pos==0)
				{
					fromLatitute=MainAppActivity.orginalLatl.latitude;
					fromLongtitute=MainAppActivity.orginalLatl.longitude;
					intent.putExtra(ApplicationConstant.NAME1,MainAppActivity.nowAddress);
				}
				else{
					MyLocate locate=myLocates.get(pos-1);
					fromLatitute=locate.getLa();
					fromLongtitute=locate.getLog();
					intent.putExtra(ApplicationConstant.NAME1,locate.getName());
				}
				intent.putExtra(ApplicationConstant.ACTION, ApplicationConstant.GET_DIRECTION_VALUE);
				intent.putExtra(ApplicationConstant.LAT1, fromLatitute);
				intent.putExtra(ApplicationConstant.LOG1, fromLongtitute);
				if(MY_LOCATE_MARK==false){
					intent.putExtra(ApplicationConstant.LAT2, LocateDetailsFragment.locate.getLa());
					intent.putExtra(ApplicationConstant.LOG2, LocateDetailsFragment.locate.getLog());
					intent.putExtra(ApplicationConstant.NAME2,LocateDetailsFragment.locate.getName());
				}
				else{
					
					intent.putExtra(ApplicationConstant.LAT2, la);
					intent.putExtra(ApplicationConstant.LOG2, log);
					intent.putExtra(ApplicationConstant.NAME2,name);
				}
				startActivity(intent);
			}
		});
	}
}
